import { createServer } from "f2e-server3"
import { Command } from "f2e-server3/lib/utils/commander"
import { MongoClient } from "mongodb"
import { createMiddleRBAC, createServiceFile, createServiceMongo } from "./index"

const command = new Command('f2e-middle-rbac')

command
    .option('-m, --mode <mode>')
    .option('-l, --load_page <load_page>')
    .option('-P, --port <port>')
    .option('-d, --debug <debug>')
    .option('-t, --type <type>')
    .action((options) => {
        const { mode, load_page, port, type = 'file' } = options
        const service = type === 'file'
            ? createServiceFile({})
            : createServiceMongo({
                db: new MongoClient("mongodb://localhost:27017").db("rbac"),
            })
        createServer({
            mode: mode === 'build' ? 'build' : 'dev',
            port: port && Number(port) || 9120,
            buildFilter: pathname => /index/.test(pathname),
            try_files: "index.html",
            middlewares: [
                createMiddleRBAC(service, { load_page: load_page === 'true', logout_url: '/logout' }),
            ],
            auth: {
                store: service.userService,
                redirect: '/admin',
            },
            dest: "public",
        })
    })

command.parse(process.argv)